package elephant.task;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author icecooly
 *
 */
public abstract class BaseTask implements Runnable{
	private static Logger logger=LoggerFactory.getLogger(BaseTask.class);
	//
	protected String id;
	protected long initialDelay;
	protected long period;
	protected TimeUnit unit;
	private Date lastRunTime;
	private int runTimes;
	//
	public BaseTask() {
	}
	//
	public abstract void runTask()throws Exception;
	//
	@Override
	public void run() {
		runTimes++;
		lastRunTime=new Date();
		if(logger.isDebugEnabled()){
			logger.debug(">>>runTask:"+getClass().getSimpleName()+" ");
		}
		try{
			runTask();
		}catch (Throwable e) {
			logger.error(getClass().getSimpleName()+" run:"+e.getMessage(),e);
		}finally{
			if(logger.isDebugEnabled()){
				logger.debug("<<<runTask:"+getClass().getSimpleName());			
			}
		}
	}
	//
	public String getId() {
		return id;
	}
	public long getInitialDelay() {
		return initialDelay;
	}
	public long getPeriod() {
		return period;
	}
	public TimeUnit getUnit() {
		return unit;
	}
	public Date getLastRunTime() {
		return lastRunTime;
	}
	public int getRunTimes() {
		return runTimes;
	}
}
